package com.app.milkteaapp.utils;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {
    /**
     * 构建一个GET请求方式的call,返回的call需在线程中调用
     * @param url 请求地址
     * @return 返回call
     */
    public static Call MyCall(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        return call;
    }

    /**
     * 构建一个POST请求方式的call,返回的call需在线程中调用
     * @param url
     * @param jsonData
     * @return
     */
    public static Call MyCall(String url,String jsonData) {
        OkHttpClient client = new OkHttpClient();

        //构建requestBody
        MediaType type = MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody = RequestBody.create(type, jsonData);

        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();

        Call call = client.newCall(request);

        return call;
    }

}
